Latent Features for Recommendations

The goal of this problem is to implement the Stochastic Gradient Descent algorithm to build a Latent Factor Recommendation system. We can use it to recommend movies to users.

Suppose we are given a matrix R of recommendations. The element $R_{iu}$ of this matrix corresponds to the rating given by user $u$ to item $i$. The size of $R$ is $m × n$, where $m$ is the number of movies, and $n$ the numbers of users. Most of the elements of the matrix are unknown because each user can only rate a few movies. Our goal is to find two matrices $P$ and $Q$, such that $R ≃ QPT$. The dimensions of $Q$ are $m × k$, and the dimensions of $P$ are $n × k$. $k$ is a parameter of the algorithm.

We defined the error as $$ E = \sum\limits_{(i,u) \in \ ratings} (R_{iu} - q_i \cdot p^T_u)^2 + \lambda [\sum\limits_{u}\|p_u\|^{2}_{2} + \sum\limits_{i}\|q_i\|^{2}_{2}]$$

The $\sum\limits_{(i,u) \in \ ratings}$ means that we sum only on the pairs (user, item) for which the user has rated the item, i.e. the ($i, u$) entry of the matrix $R$ is known. $q_i$ denotes the $i^{th}$ row of the matrix $Q$ (corresponding to an item), and $p_u$ the $u^{th}$ row of the matrix $P$ (corresponding to a user $u$). $\lambda$ is the regularization parameter. $\|\cdot\|_{2}$ is the $L_2$ norm and $\|p_u\|^{2}_{2}$ is square of the $L_2$ norm, i.e., it is the sum of squares of elements of $p_u$.

Part A

What is $\varepsilon_{iu}$? What are the update equations for $q_i$ and $p_u$ in the Stochastic Gradient Decent algorithm? Here, $\varepsilon_{iu}$ denotes the derivative of the error $E$ with respect to $R_{iu}$.

Ans: $$\varepsilon_{iu} = R_{iu} - q_i \cdot p^T_u$$ $$q_i := q_i + \eta \ast (\varepsilon_{iu} \ast p_u - \lambda \ast q_i)$$ $$p_u := p_u + \eta \ast (\varepsilon_{iu} \ast q_i - \lambda \ast p_u)$$

Part B

Implement the algorithm. Read each entry of the matrix $R$ from disk and update $\varepsilon_{iu}$, $q_i$ and $p_u$ for each entry. You have to read each element $R_{iu}$ at a time from disk and apply your update equations (to each element). Then, iterate until both $q_i$ and $p_u$ stop changing. Each iteration of the algorithm will read the whole file.

Choose $k$ = 20, $\lambda$ = 0.2 and number of iterations = 40. Find a good value for the learning rate $\eta$. Start with $\eta$ = 0.1. The error $E$ on the training set should be less than 83000 after 40 iterations.

Use the following files:

  • ratings.train.txt: This is the matrix $R$. Each entry is made of a user id, a movie id, and a rating.
  • ratings.val.txt: This is the test set. You will use it to evaluate your recommenda- tion system. It consists of entries of the matrix that were removed from the original dataset to create the training set.

Plot the value of the objective function $E$ on the training set as a function of the number of iterations. What value of $\eta$ did you find?

Ans: $\eta$ = 0.03


In [10]:
%pylab inline
from __future__ import division
import numpy
import random
import math
import pylab


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['pylab', 'random']
`%matplotlib` prevents importing * from pylab and numpy

In [2]:
TRAIN_FILE = "ratings.train.txt"
TEST_FILE = "ratings.val.txt"

In [3]:
def get_matrix_dims(fileName):
    maxUserId = 0
    maxMovieId = 0
    data = open(fileName,"r")
    count = 0
    for line in data:
        count += 1
        [userId, movieId, rating] = line.split("\t")
        if int(movieId) > maxMovieId:
            maxMovieId  = int(movieId)

        if int(userId) > maxUserId: 
            maxUserId  = int(userId)
    data.close()
            
    return (maxUserId, maxMovieId)

In [4]:
def create_random_matrix(rows, cols):
    tmp = numpy.array([random.gauss(0, math.sqrt(5/cols)) for i in xrange(rows*cols)])
    tmp.shape = (rows,cols)
    return tmp

In [5]:
def total_error(P, Q, RFile, lam):
    data = open(RFile,"r")
    err = 0.0
    sumPL2Norm = sum([numpy.dot(i,i) for i in P])
    sumQL2Norm = sum([numpy.dot(i,i) for i in Q])
    
    for line in data:
        [u, i, r] = line.split("\t")
        u = int(u) - 1
        i = int(i) - 1
        r = float(r)
        err = err + math.pow((r - numpy.dot(Q[i,:], P[u,:])),2)
        
    err = err + lam * (sumPL2Norm + sumQL2Norm)
    
    return err

In [6]:
def t_error(P, Q, RFile):
    data = open(RFile,"r")
    err = 0.0
    
    for line in data:
        [u, i, r] = line.split("\t")
        u = int(u) - 1
        i = int(i) - 1
        r = float(r)
        err = err + math.pow((r - numpy.dot(Q[i,:], P[u,:])),2)
    
    return err

In [7]:
def stochastic_gradient_descent(P, Q, RFile, iter, lam, eta):
    print "Running SGD for %s iterations." % iter
    boom = False
    error = []
    for j in xrange(iter):
        if boom: break
        print "Iteration :",j, "...",
        data = open(RFile,"r")
        for line in data: 
            # Get u , m , r
            [u, i, r] = line.split("\t")
            # Numpy indexes array from 0
            u = int(u) - 1
            i = int(i) - 1
            r = float(r)

            if math.isnan(numpy.dot(Q[i,:], P[u,:])):
                errorTxt =  "(Iteration %s :Nan at u: %s i: %s r: %s )" \
                                % (j,u,i,r)
                boom = True
                raise Exception(errorTxt)
              
            # Calculate Error
            e = r - numpy.dot(Q[i,:], P[u,:]) 

            #Update q
            tmp_q = Q[i,:] + eta*(e*P[u,:] - lam*Q[i,:])

            #Update p
            tmp_p = P[u,:] + eta*(e*Q[i,:] - lam*P[u,:])
       
            Q[i,:] = tmp_q
            P[u,:] = tmp_p
           
        data.close() 
        print "done.",
        error.append(total_error(P,Q,RFile,lam))
        print "Error:[ %s ]" % (error[j])
    return P, Q, error

In [8]:
(u,m) = get_matrix_dims(TRAIN_FILE)

In [9]:
k = 20
iter = 40
lam = 0.2

eta = 0.03
P = create_random_matrix(u, k)
Q = create_random_matrix(m, k) 
P1, Q1, e1 = stochastic_gradient_descent(P,Q,TRAIN_FILE,iter,lam,eta)


Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 95499.5874623 ]
Iteration : 1 ... done. Error:[ 86000.7261113 ]
Iteration : 2 ... done. Error:[ 84158.3631837 ]
Iteration : 3 ... done. Error:[ 83343.3261789 ]
Iteration : 4 ... done. Error:[ 82816.7717895 ]
Iteration : 5 ... done. Error:[ 82425.4314979 ]
Iteration : 6 ... done. Error:[ 82120.1133277 ]
Iteration : 7 ... done. Error:[ 81875.0196835 ]
Iteration : 8 ... done. Error:[ 81672.5153525 ]
Iteration : 9 ... done. Error:[ 81499.9223149 ]
Iteration : 10 ... done. Error:[ 81348.2929653 ]
Iteration : 11 ... done. Error:[ 81211.5068255 ]
Iteration : 12 ... done. Error:[ 81085.5182818 ]
Iteration : 13 ... done. Error:[ 80967.7549726 ]
Iteration : 14 ... done. Error:[ 80856.6555789 ]
Iteration : 15 ... done. Error:[ 80751.3241896 ]
Iteration : 16 ... done. Error:[ 80651.2779852 ]
Iteration : 17 ... done. Error:[ 80556.2686128 ]
Iteration : 18 ... done. Error:[ 80466.1612168 ]
Iteration : 19 ... done. Error:[ 80380.8577025 ]
Iteration : 20 ... done. Error:[ 80300.2527475 ]
Iteration : 21 ... done. Error:[ 80224.2128138 ]
Iteration : 22 ... done. Error:[ 80152.5701921 ]
Iteration : 23 ... done. Error:[ 80085.1259164 ]
Iteration : 24 ... done. Error:[ 80021.6571075 ]
Iteration : 25 ... done. Error:[ 79961.9257861 ]
Iteration : 26 ... done. Error:[ 79905.6873782 ]
Iteration : 27 ... done. Error:[ 79852.6979884 ]
Iteration : 28 ... done. Error:[ 79802.7200908 ]
Iteration : 29 ... done. Error:[ 79755.5266308 ]
Iteration : 30 ... done. Error:[ 79710.9037143 ]
Iteration : 31 ... done. Error:[ 79668.6521346 ]
Iteration : 32 ... done. Error:[ 79628.5880008 ]
Iteration : 33 ... done. Error:[ 79590.5427065 ]
Iteration : 34 ... done. Error:[ 79554.362441 ]
Iteration : 35 ... done. Error:[ 79519.9074044 ]
Iteration : 36 ... done. Error:[ 79487.0508481 ]
Iteration : 37 ... done. Error:[ 79455.6780323 ]
Iteration : 38 ... done. Error:[ 79425.6851629 ]
Iteration : 39 ... done. Error:[ 79396.9783515 ]

In [11]:
f0 = pylab.figure()
p1 = f0.add_subplot(111)
p1.plot([i for i in xrange(iter)],e1) 
p1.set_xlabel("Iteration")
p1.set_ylabel("Error")
p1.set_title("Gradient Descent : Error vs. Iterations")


Out[11]:
<matplotlib.text.Text at 0x1061dc6d0>

Part C

We define the training error as: $$E_{tr} = \sum\limits_{(i,u) \in \ ratings} (R_{iu} - q_i \cdot p^T_u)^2$$

and the test error as: $$E_{te} = \sum\limits_{(i,u) \in \ test ratings} (R_{iu} - q_i \cdot p^T_u)^2$$

Note that these equations do not include the regularization penalty that $E$ had from parts a and b. Plot $E_{te}$ and $E_{tr}$ as a function of $k \in {1,2,...10}$ for the following values of $\lambda$:

  • $\lambda$ = 0.0 (Part C.i)
  • $\lambda$ = 0.2 (Part C.ii)

For $\eta$ use the value found in Part B. Based on the output, identify the valid statements.

Ans:

  • Regularization decreases the test error for $k \ge$ 5
  • Regularization increases the training error for all (or almost all) $k$
  • Regularization decreases overfitting

Part C.i


In [12]:
K = [i+1 for i in xrange(10)]

e_tot = [0 for i in K]
e_trn = [0 for i in K]
e_tst = [0 for i in K]
count = 0
eta = 0.03
lam = 0.0

In [13]:
for j in K:
    print "k = ",j
    P = create_random_matrix(u, j)
    Q = create_random_matrix(m, j)     
    X, Y, E = stochastic_gradient_descent(P,Q,TRAIN_FILE,iter,lam,eta)
    e_tot[count] = E.pop()
    e_trn[count]= t_error(X,Y,TRAIN_FILE)
    e_tst[count]= t_error(X,Y,TEST_FILE)
    print "Error[ Total :%s Training: %s Test: %s]" % \
                (e_tot[count],e_trn[count],e_tst[count])
    count = count + 1


k =  1
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 136759.455271 ]
Iteration : 1 ... done. Error:[ 96573.6092883 ]
Iteration : 2 ... done. Error:[ 90072.7930637 ]
Iteration : 3 ... done. Error:[ 87573.049329 ]
Iteration : 4 ... done. Error:[ 86325.2648696 ]
Iteration : 5 ... done. Error:[ 85613.3865923 ]
Iteration : 6 ... done. Error:[ 85170.2056962 ]
Iteration : 7 ... done. Error:[ 84875.4234431 ]
Iteration : 8 ... done. Error:[ 84668.3684783 ]
Iteration : 9 ... done. Error:[ 84516.1458324 ]
Iteration : 10 ... done. Error:[ 84399.9804438 ]
Iteration : 11 ... done. Error:[ 84308.6778821 ]
Iteration : 12 ... done. Error:[ 84235.2711301 ]
Iteration : 13 ... done. Error:[ 84175.2287853 ]
Iteration : 14 ... done. Error:[ 84125.4706226 ]
Iteration : 15 ... done. Error:[ 84083.8137734 ]
Iteration : 16 ... done. Error:[ 84048.6532935 ]
Iteration : 17 ... done. Error:[ 84018.7724987 ]
Iteration : 18 ... done. Error:[ 83993.226447 ]
Iteration : 19 ... done. Error:[ 83971.2675472 ]
Iteration : 20 ... done. Error:[ 83952.2960811 ]
Iteration : 21 ... done. Error:[ 83935.8259386 ]
Iteration : 22 ... done. Error:[ 83921.4599907 ]
Iteration : 23 ... done. Error:[ 83908.8718163 ]
Iteration : 24 ... done. Error:[ 83897.7917893 ]
Iteration : 25 ... done. Error:[ 83887.99627 ]
Iteration : 26 ... done. Error:[ 83879.2990814 ]
Iteration : 27 ... done. Error:[ 83871.5447105 ]
Iteration : 28 ... done. Error:[ 83864.602841 ]
Iteration : 29 ... done. Error:[ 83858.3639294 ]
Iteration : 30 ... done. Error:[ 83852.7356073 ]
Iteration : 31 ... done. Error:[ 83847.6397462 ]
Iteration : 32 ... done. Error:[ 83843.0100532 ]
Iteration : 33 ... done. Error:[ 83838.7900984 ]
Iteration : 34 ... done. Error:[ 83834.9316909 ]
Iteration : 35 ... done. Error:[ 83831.3935396 ]
Iteration : 36 ... done. Error:[ 83828.1401461 ]
Iteration : 37 ... done. Error:[ 83825.1408883 ]
Iteration : 38 ... done. Error:[ 83822.3692587 ]
Iteration : 39 ... done. Error:[ 83819.8022318 ]
Error[ Total :83819.8022318 Training: 83819.8022318 Test: 9971.09446373]
k =  2
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 117580.061134 ]
Iteration : 1 ... done. Error:[ 92526.1369365 ]
Iteration : 2 ... done. Error:[ 87552.5342174 ]
Iteration : 3 ... done. Error:[ 85310.8068129 ]
Iteration : 4 ... done. Error:[ 83905.3768825 ]
Iteration : 5 ... done. Error:[ 82825.3306906 ]
Iteration : 6 ... done. Error:[ 81903.0373475 ]
Iteration : 7 ... done. Error:[ 81087.1455701 ]
Iteration : 8 ... done. Error:[ 80364.3797939 ]
Iteration : 9 ... done. Error:[ 79729.674296 ]
Iteration : 10 ... done. Error:[ 79176.8731518 ]
Iteration : 11 ... done. Error:[ 78697.5142034 ]
Iteration : 12 ... done. Error:[ 78281.9537649 ]
Iteration : 13 ... done. Error:[ 77920.6729754 ]
Iteration : 14 ... done. Error:[ 77605.2081526 ]
Iteration : 15 ... done. Error:[ 77328.6197072 ]
Iteration : 16 ... done. Error:[ 77085.5375903 ]
Iteration : 17 ... done. Error:[ 76871.8969375 ]
Iteration : 18 ... done. Error:[ 76684.5282379 ]
Iteration : 19 ... done. Error:[ 76520.7646516 ]
Iteration : 20 ... done. Error:[ 76378.1689189 ]
Iteration : 21 ... done. Error:[ 76254.4009705 ]
Iteration : 22 ... done. Error:[ 76147.1919446 ]
Iteration : 23 ... done. Error:[ 76054.3754173 ]
Iteration : 24 ... done. Error:[ 75973.936346 ]
Iteration : 25 ... done. Error:[ 75904.0536113 ]
Iteration : 26 ... done. Error:[ 75843.1248835 ]
Iteration : 27 ... done. Error:[ 75789.771654 ]
Iteration : 28 ... done. Error:[ 75742.8278819 ]
Iteration : 29 ... done. Error:[ 75701.3181073 ]
Iteration : 30 ... done. Error:[ 75664.4308339 ]
Iteration : 31 ... done. Error:[ 75631.4916137 ]
Iteration : 32 ... done. Error:[ 75601.9385447 ]
Iteration : 33 ... done. Error:[ 75575.3014236 ]
Iteration : 34 ... done. Error:[ 75551.1847654 ]
Iteration : 35 ... done. Error:[ 75529.2543165 ]
Iteration : 36 ... done. Error:[ 75509.2264397 ]
Iteration : 37 ... done. Error:[ 75490.8597179 ]
Iteration : 38 ... done. Error:[ 75473.9482146 ]
Iteration : 39 ... done. Error:[ 75458.3159601 ]
Error[ Total :75458.3159601 Training: 75458.3159601 Test: 9469.67378819]
k =  3
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 110039.947352 ]
Iteration : 1 ... done. Error:[ 89820.3375272 ]
Iteration : 2 ... done. Error:[ 85660.7917761 ]
Iteration : 3 ... done. Error:[ 83809.426877 ]
Iteration : 4 ... done. Error:[ 82687.8854437 ]
Iteration : 5 ... done. Error:[ 81842.0246126 ]
Iteration : 6 ... done. Error:[ 81086.7563156 ]
Iteration : 7 ... done. Error:[ 80338.9072964 ]
Iteration : 8 ... done. Error:[ 79573.1283532 ]
Iteration : 9 ... done. Error:[ 78801.4184665 ]
Iteration : 10 ... done. Error:[ 78052.6414755 ]
Iteration : 11 ... done. Error:[ 77354.0538797 ]
Iteration : 12 ... done. Error:[ 76721.305016 ]
Iteration : 13 ... done. Error:[ 76157.7855385 ]
Iteration : 14 ... done. Error:[ 75659.0476008 ]
Iteration : 15 ... done. Error:[ 75217.5157027 ]
Iteration : 16 ... done. Error:[ 74825.3853214 ]
Iteration : 17 ... done. Error:[ 74475.8336584 ]
Iteration : 18 ... done. Error:[ 74163.2766934 ]
Iteration : 19 ... done. Error:[ 73883.2207503 ]
Iteration : 20 ... done. Error:[ 73631.9929104 ]
Iteration : 21 ... done. Error:[ 73406.4814205 ]
Iteration : 22 ... done. Error:[ 73203.9442738 ]
Iteration : 23 ... done. Error:[ 73021.9015874 ]
Iteration : 24 ... done. Error:[ 72858.0952004 ]
Iteration : 25 ... done. Error:[ 72710.4806559 ]
Iteration : 26 ... done. Error:[ 72577.2194659 ]
Iteration : 27 ... done. Error:[ 72456.6582941 ]
Iteration : 28 ... done. Error:[ 72347.3009105 ]
Iteration : 29 ... done. Error:[ 72247.7858333 ]
Iteration : 30 ... done. Error:[ 72156.8770026 ]
Iteration : 31 ... done. Error:[ 72073.4652009 ]
Iteration : 32 ... done. Error:[ 71996.572484 ]
Iteration : 33 ... done. Error:[ 71925.3526506 ]
Iteration : 34 ... done. Error:[ 71859.0848133 ]
Iteration : 35 ... done. Error:[ 71797.1608697 ]
Iteration : 36 ... done. Error:[ 71739.069519 ]
Iteration : 37 ... done. Error:[ 71684.3796091 ]
Iteration : 38 ... done. Error:[ 71632.7248726 ]
Iteration : 39 ... done. Error:[ 71583.7911773 ]
Error[ Total :71583.7911773 Training: 71583.7911773 Test: 9604.66411146]
k =  4
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 104560.559901 ]
Iteration : 1 ... done. Error:[ 88098.7658691 ]
Iteration : 2 ... done. Error:[ 84229.4551164 ]
Iteration : 3 ... done. Error:[ 82291.333476 ]
Iteration : 4 ... done. Error:[ 80863.9141027 ]
Iteration : 5 ... done. Error:[ 79547.8263482 ]
Iteration : 6 ... done. Error:[ 78249.6557401 ]
Iteration : 7 ... done. Error:[ 77006.0605445 ]
Iteration : 8 ... done. Error:[ 75867.584802 ]
Iteration : 9 ... done. Error:[ 74855.0777786 ]
Iteration : 10 ... done. Error:[ 73966.7421343 ]
Iteration : 11 ... done. Error:[ 73191.0666908 ]
Iteration : 12 ... done. Error:[ 72514.5042611 ]
Iteration : 13 ... done. Error:[ 71924.4125209 ]
Iteration : 14 ... done. Error:[ 71409.4721309 ]
Iteration : 15 ... done. Error:[ 70959.4464265 ]
Iteration : 16 ... done. Error:[ 70565.0536178 ]
Iteration : 17 ... done. Error:[ 70218.0135748 ]
Iteration : 18 ... done. Error:[ 69911.1336081 ]
Iteration : 19 ... done. Error:[ 69638.3319208 ]
Iteration : 20 ... done. Error:[ 69394.5725671 ]
Iteration : 21 ... done. Error:[ 69175.7344221 ]
Iteration : 22 ... done. Error:[ 68978.451834 ]
Iteration : 23 ... done. Error:[ 68799.9587547 ]
Iteration : 24 ... done. Error:[ 68637.954825 ]
Iteration : 25 ... done. Error:[ 68490.4997024 ]
Iteration : 26 ... done. Error:[ 68355.9341227 ]
Iteration : 27 ... done. Error:[ 68232.8226855 ]
Iteration : 28 ... done. Error:[ 68119.9127361 ]
Iteration : 29 ... done. Error:[ 68016.1045203 ]
Iteration : 30 ... done. Error:[ 67920.4290106 ]
Iteration : 31 ... done. Error:[ 67832.0309176 ]
Iteration : 32 ... done. Error:[ 67750.1552203 ]
Iteration : 33 ... done. Error:[ 67674.1360923 ]
Iteration : 34 ... done. Error:[ 67603.3874364 ]
Iteration : 35 ... done. Error:[ 67537.3944636 ]
Iteration : 36 ... done. Error:[ 67475.7059117 ]
Iteration : 37 ... done. Error:[ 67417.9266379 ]
Iteration : 38 ... done. Error:[ 67363.7104391 ]
Iteration : 39 ... done. Error:[ 67312.7530733 ]
Error[ Total :67312.7530733 Training: 67312.7530733 Test: 9991.88440108]
k =  5
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 102405.07066 ]
Iteration : 1 ... done. Error:[ 86450.7951703 ]
Iteration : 2 ... done. Error:[ 82249.9154848 ]
Iteration : 3 ... done. Error:[ 79728.111616 ]
Iteration : 4 ... done. Error:[ 77733.5631058 ]
Iteration : 5 ... done. Error:[ 76054.7865391 ]
Iteration : 6 ... done. Error:[ 74626.2122619 ]
Iteration : 7 ... done. Error:[ 73401.2303395 ]
Iteration : 8 ... done. Error:[ 72343.2910478 ]
Iteration : 9 ... done. Error:[ 71424.2660858 ]
Iteration : 10 ... done. Error:[ 70621.8342999 ]
Iteration : 11 ... done. Error:[ 69917.8329528 ]
Iteration : 12 ... done. Error:[ 69297.3518135 ]
Iteration : 13 ... done. Error:[ 68748.0640824 ]
Iteration : 14 ... done. Error:[ 68259.6797516 ]
Iteration : 15 ... done. Error:[ 67823.5340266 ]
Iteration : 16 ... done. Error:[ 67432.2945489 ]
Iteration : 17 ... done. Error:[ 67079.7483074 ]
Iteration : 18 ... done. Error:[ 66760.636209 ]
Iteration : 19 ... done. Error:[ 66470.5183641 ]
Iteration : 20 ... done. Error:[ 66205.6622775 ]
Iteration : 21 ... done. Error:[ 65962.9485224 ]
Iteration : 22 ... done. Error:[ 65739.7883203 ]
Iteration : 23 ... done. Error:[ 65534.047747 ]
Iteration : 24 ... done. Error:[ 65343.9746838 ]
Iteration : 25 ... done. Error:[ 65168.1268188 ]
Iteration : 26 ... done. Error:[ 65005.3016677 ]
Iteration : 27 ... done. Error:[ 64854.4721507 ]
Iteration : 28 ... done. Error:[ 64714.7325401 ]
Iteration : 29 ... done. Error:[ 64585.2585423 ]
Iteration : 30 ... done. Error:[ 64465.282205 ]
Iteration : 31 ... done. Error:[ 64354.0790454 ]
Iteration : 32 ... done. Error:[ 64250.9631157 ]
Iteration : 33 ... done. Error:[ 64155.2860872 ]
Iteration : 34 ... done. Error:[ 64066.437861 ]
Iteration : 35 ... done. Error:[ 63983.8475296 ]
Iteration : 36 ... done. Error:[ 63906.9842137 ]
Iteration : 37 ... done. Error:[ 63835.3575381 ]
Iteration : 38 ... done. Error:[ 63768.5175772 ]
Iteration : 39 ... done. Error:[ 63706.054176 ]
Error[ Total :63706.054176 Training: 63706.054176 Test: 10149.4486869]
k =  6
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 101702.64816 ]
Iteration : 1 ... done. Error:[ 85359.5778953 ]
Iteration : 2 ... done. Error:[ 80929.9133286 ]
Iteration : 3 ... done. Error:[ 78290.0867397 ]
Iteration : 4 ... done. Error:[ 76179.0155381 ]
Iteration : 5 ... done. Error:[ 74366.6747278 ]
Iteration : 6 ... done. Error:[ 72812.2383871 ]
Iteration : 7 ... done. Error:[ 71484.826398 ]
Iteration : 8 ... done. Error:[ 70350.1942734 ]
Iteration : 9 ... done. Error:[ 69378.4536801 ]
Iteration : 10 ... done. Error:[ 68545.0064915 ]
Iteration : 11 ... done. Error:[ 67828.8115494 ]
Iteration : 12 ... done. Error:[ 67211.3270967 ]
Iteration : 13 ... done. Error:[ 66676.3191374 ]
Iteration : 14 ... done. Error:[ 66209.9423112 ]
Iteration : 15 ... done. Error:[ 65800.7092972 ]
Iteration : 16 ... done. Error:[ 65439.2728229 ]
Iteration : 17 ... done. Error:[ 65118.0966322 ]
Iteration : 18 ... done. Error:[ 64831.1069532 ]
Iteration : 19 ... done. Error:[ 64573.3794763 ]
Iteration : 20 ... done. Error:[ 64340.8820848 ]
Iteration : 21 ... done. Error:[ 64130.2741138 ]
Iteration : 22 ... done. Error:[ 63938.7549291 ]
Iteration : 23 ... done. Error:[ 63763.952295 ]
Iteration : 24 ... done. Error:[ 63603.840971 ]
Iteration : 25 ... done. Error:[ 63456.6829013 ]
Iteration : 26 ... done. Error:[ 63320.9818298 ]
Iteration : 27 ... done. Error:[ 63195.4469917 ]
Iteration : 28 ... done. Error:[ 63078.9624558 ]
Iteration : 29 ... done. Error:[ 62970.5604085 ]
Iteration : 30 ... done. Error:[ 62869.3979104 ]
Iteration : 31 ... done. Error:[ 62774.7372526 ]
Iteration : 32 ... done. Error:[ 62685.9300673 ]
Iteration : 33 ... done. Error:[ 62602.404995 ]
Iteration : 34 ... done. Error:[ 62523.6582848 ]
Iteration : 35 ... done. Error:[ 62449.2464226 ]
Iteration : 36 ... done. Error:[ 62378.7798699 ]
Iteration : 37 ... done. Error:[ 62311.9172154 ]
Iteration : 38 ... done. Error:[ 62248.359379 ]
Iteration : 39 ... done. Error:[ 62187.8438313 ]
Error[ Total :62187.8438313 Training: 62187.8438313 Test: 10578.6273589]
k =  7
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 100885.705179 ]
Iteration : 1 ... done. Error:[ 84441.8132106 ]
Iteration : 2 ... done. Error:[ 79743.3450217 ]
Iteration : 3 ... done. Error:[ 76790.0448292 ]
Iteration : 4 ... done. Error:[ 74436.7423262 ]
Iteration : 5 ... done. Error:[ 72496.0074177 ]
Iteration : 6 ... done. Error:[ 70893.5148117 ]
Iteration : 7 ... done. Error:[ 69563.1021605 ]
Iteration : 8 ... done. Error:[ 68449.7210888 ]
Iteration : 9 ... done. Error:[ 67509.0754755 ]
Iteration : 10 ... done. Error:[ 66705.7335815 ]
Iteration : 11 ... done. Error:[ 66011.6770554 ]
Iteration : 12 ... done. Error:[ 65405.047194 ]
Iteration : 13 ... done. Error:[ 64869.0003926 ]
Iteration : 14 ... done. Error:[ 64390.6821662 ]
Iteration : 15 ... done. Error:[ 63960.3201291 ]
Iteration : 16 ... done. Error:[ 63570.4452958 ]
Iteration : 17 ... done. Error:[ 63215.2599024 ]
Iteration : 18 ... done. Error:[ 62890.1598261 ]
Iteration : 19 ... done. Error:[ 62591.3992373 ]
Iteration : 20 ... done. Error:[ 62315.8684642 ]
Iteration : 21 ... done. Error:[ 62060.9502326 ]
Iteration : 22 ... done. Error:[ 61824.4236109 ]
Iteration : 23 ... done. Error:[ 61604.3942369 ]
Iteration : 24 ... done. Error:[ 61399.2388007 ]
Iteration : 25 ... done. Error:[ 61207.558477 ]
Iteration : 26 ... done. Error:[ 61028.1394944 ]
Iteration : 27 ... done. Error:[ 60859.9201736 ]
Iteration : 28 ... done. Error:[ 60701.9638012 ]
Iteration : 29 ... done. Error:[ 60553.4365419 ]
Iteration : 30 ... done. Error:[ 60413.5896067 ]
Iteration : 31 ... done. Error:[ 60281.7450652 ]
Iteration : 32 ... done. Error:[ 60157.2848599 ]
Iteration : 33 ... done. Error:[ 60039.6426392 ]
Iteration : 34 ... done. Error:[ 59928.2979621 ]
Iteration : 35 ... done. Error:[ 59822.7723261 ]
Iteration : 36 ... done. Error:[ 59722.6264054 ]
Iteration : 37 ... done. Error:[ 59627.4579228 ]
Iteration : 38 ... done. Error:[ 59536.8996964 ]
Iteration : 39 ... done. Error:[ 59450.6175791 ]
Error[ Total :59450.6175791 Training: 59450.6175791 Test: 10701.4939511]
k =  8
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 98425.1718179 ]
Iteration : 1 ... done. Error:[ 83024.3351074 ]
Iteration : 2 ... done. Error:[ 78037.1761253 ]
Iteration : 3 ... done. Error:[ 74690.3445148 ]
Iteration : 4 ... done. Error:[ 72065.3207403 ]
Iteration : 5 ... done. Error:[ 69936.5695847 ]
Iteration : 6 ... done. Error:[ 68179.2859785 ]
Iteration : 7 ... done. Error:[ 66708.1876595 ]
Iteration : 8 ... done. Error:[ 65462.1326912 ]
Iteration : 9 ... done. Error:[ 64395.1304608 ]
Iteration : 10 ... done. Error:[ 63471.6876689 ]
Iteration : 11 ... done. Error:[ 62664.6434523 ]
Iteration : 12 ... done. Error:[ 61953.6965609 ]
Iteration : 13 ... done. Error:[ 61323.9041049 ]
Iteration : 14 ... done. Error:[ 60764.1716169 ]
Iteration : 15 ... done. Error:[ 60265.9473588 ]
Iteration : 16 ... done. Error:[ 59822.2539793 ]
Iteration : 17 ... done. Error:[ 59427.0650069 ]
Iteration : 18 ... done. Error:[ 59074.9589217 ]
Iteration : 19 ... done. Error:[ 58760.963803 ]
Iteration : 20 ... done. Error:[ 58480.513822 ]
Iteration : 21 ... done. Error:[ 58229.4564257 ]
Iteration : 22 ... done. Error:[ 58004.0691302 ]
Iteration : 23 ... done. Error:[ 57801.0640122 ]
Iteration : 24 ... done. Error:[ 57617.5731038 ]
Iteration : 25 ... done. Error:[ 57451.1172117 ]
Iteration : 26 ... done. Error:[ 57299.5645574 ]
Iteration : 27 ... done. Error:[ 57161.0857905 ]
Iteration : 28 ... done. Error:[ 57034.110282 ]
Iteration : 29 ... done. Error:[ 56917.286561 ]
Iteration : 30 ... done. Error:[ 56809.4480677 ]
Iteration : 31 ... done. Error:[ 56709.5842806 ]
Iteration : 32 ... done. Error:[ 56616.8166883 ]
Iteration : 33 ... done. Error:[ 56530.3788628 ]
Iteration : 34 ... done. Error:[ 56449.5998971 ]
Iteration : 35 ... done. Error:[ 56373.8905743 ]
Iteration : 36 ... done. Error:[ 56302.7317654 ]
Iteration : 37 ... done. Error:[ 56235.6646706 ]
Iteration : 38 ... done. Error:[ 56172.2826077 ]
Iteration : 39 ... done. Error:[ 56112.2241112 ]
Error[ Total :56112.2241112 Training: 56112.2241112 Test: 10994.3709828]
k =  9
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 100281.024003 ]
Iteration : 1 ... done. Error:[ 82869.6141237 ]
Iteration : 2 ... done. Error:[ 77263.1466482 ]
Iteration : 3 ... done. Error:[ 73718.6941122 ]
Iteration : 4 ... done. Error:[ 71071.3259581 ]
Iteration : 5 ... done. Error:[ 68948.0261257 ]
Iteration : 6 ... done. Error:[ 67179.8080523 ]
Iteration : 7 ... done. Error:[ 65678.5802351 ]
Iteration : 8 ... done. Error:[ 64389.3441412 ]
Iteration : 9 ... done. Error:[ 63272.2556933 ]
Iteration : 10 ... done. Error:[ 62296.417902 ]
Iteration : 11 ... done. Error:[ 61437.802953 ]
Iteration : 12 ... done. Error:[ 60677.9856068 ]
Iteration : 13 ... done. Error:[ 60002.7658661 ]
Iteration : 14 ... done. Error:[ 59400.8456848 ]
Iteration : 15 ... done. Error:[ 58862.8319071 ]
Iteration : 16 ... done. Error:[ 58380.6325005 ]
Iteration : 17 ... done. Error:[ 57947.1552406 ]
Iteration : 18 ... done. Error:[ 57556.1746143 ]
Iteration : 19 ... done. Error:[ 57202.2619776 ]
Iteration : 20 ... done. Error:[ 56880.7231264 ]
Iteration : 21 ... done. Error:[ 56587.5256872 ]
Iteration : 22 ... done. Error:[ 56319.2183708 ]
Iteration : 23 ... done. Error:[ 56072.8497273 ]
Iteration : 24 ... done. Error:[ 55845.8927173 ]
Iteration : 25 ... done. Error:[ 55636.1782961 ]
Iteration : 26 ... done. Error:[ 55441.8386805 ]
Iteration : 27 ... done. Error:[ 55261.2595848 ]
Iteration : 28 ... done. Error:[ 55093.0402271 ]
Iteration : 29 ... done. Error:[ 54935.9598977 ]
Iteration : 30 ... done. Error:[ 54788.9500588 ]
Iteration : 31 ... done. Error:[ 54651.0711299 ]
Iteration : 32 ... done. Error:[ 54521.4932636 ]
Iteration : 33 ... done. Error:[ 54399.4805138 ]
Iteration : 34 ... done. Error:[ 54284.3778645 ]
Iteration : 35 ... done. Error:[ 54175.6006338 ]
Iteration : 36 ... done. Error:[ 54072.6258147 ]
Iteration : 37 ... done. Error:[ 53974.9849585 ]
Iteration : 38 ... done. Error:[ 53882.2582594 ]
Iteration : 39 ... done. Error:[ 53794.0695529 ]
Error[ Total :53794.0695529 Training: 53794.0695529 Test: 11357.2312232]
k =  10
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 97675.0724468 ]
Iteration : 1 ... done. Error:[ 81879.0363694 ]
Iteration : 2 ... done. Error:[ 76445.4245573 ]
Iteration : 3 ... done. Error:[ 72775.3601468 ]
Iteration : 4 ... done. Error:[ 69898.9484223 ]
Iteration : 5 ... done. Error:[ 67572.5477783 ]
Iteration : 6 ... done. Error:[ 65660.5496481 ]
Iteration : 7 ... done. Error:[ 64070.7446269 ]
Iteration : 8 ... done. Error:[ 62733.6651516 ]
Iteration : 9 ... done. Error:[ 61595.4671846 ]
Iteration : 10 ... done. Error:[ 60614.5822611 ]
Iteration : 11 ... done. Error:[ 59759.2690417 ]
Iteration : 12 ... done. Error:[ 59005.5830806 ]
Iteration : 13 ... done. Error:[ 58335.6154708 ]
Iteration : 14 ... done. Error:[ 57735.9520408 ]
Iteration : 15 ... done. Error:[ 57196.3782887 ]
Iteration : 16 ... done. Error:[ 56708.8762481 ]
Iteration : 17 ... done. Error:[ 56266.924997 ]
Iteration : 18 ... done. Error:[ 55865.0621456 ]
Iteration : 19 ... done. Error:[ 55498.6288861 ]
Iteration : 20 ... done. Error:[ 55163.6201358 ]
Iteration : 21 ... done. Error:[ 54856.5831865 ]
Iteration : 22 ... done. Error:[ 54574.5349469 ]
Iteration : 23 ... done. Error:[ 54314.8874993 ]
Iteration : 24 ... done. Error:[ 54075.3814957 ]
Iteration : 25 ... done. Error:[ 53854.0294614 ]
Iteration : 26 ... done. Error:[ 53649.0701149 ]
Iteration : 27 ... done. Error:[ 53458.9330903 ]
Iteration : 28 ... done. Error:[ 53282.2122571 ]
Iteration : 29 ... done. Error:[ 53117.6454315 ]
Iteration : 30 ... done. Error:[ 52964.0984651 ]
Iteration : 31 ... done. Error:[ 52820.5521613 ]
Iteration : 32 ... done. Error:[ 52686.0909843 ]
Iteration : 33 ... done. Error:[ 52559.8929457 ]
Iteration : 34 ... done. Error:[ 52441.2203496 ]
Iteration : 35 ... done. Error:[ 52329.4112454 ]
Iteration : 36 ... done. Error:[ 52223.8715271 ]
Iteration : 37 ... done. Error:[ 52124.067651 ]
Iteration : 38 ... done. Error:[ 52029.519958 ]
Iteration : 39 ... done. Error:[ 51939.7965865 ]
Error[ Total :51939.7965865 Training: 51939.7965865 Test: 11673.3818024]

In [18]:
x = pylab.arange(0,len(K))

f1 = pylab.figure(figsize=(9,9))
p1 = f1.add_subplot(211)
p1.plot(x, e_trn)
p1.set_xlabel("Iteration")
p1.set_ylabel("Training Error")
p1.set_title("No regularization : Training Error vs. Iterations")

p2 = f1.add_subplot(212)
p2.plot(x, e_tst)
p2.set_xlabel("Iteration")
p2.set_ylabel("Test Error")
p2.set_title("No regularization : Test Error vs. Iterations")


Out[18]:
<matplotlib.text.Text at 0x106e35890>

Part C.ii


In [19]:
e_tot_reg = [0 for i in K]
e_trn_reg = [0 for i in K]
e_tst_reg = [0 for i in K]
count = 0
eta = 0.03
lam = 0.2

In [20]:
for j in K:
    print "k = ",j
    P = create_random_matrix(u, j)
    Q = create_random_matrix(m, j)     
    X, Y, E = stochastic_gradient_descent(P,Q,TRAIN_FILE,iter,lam,eta)
    e_tot_reg[count] = E.pop()
    e_trn_reg[count]= t_error(X,Y,TRAIN_FILE)
    e_tst_reg[count]= t_error(X,Y,TEST_FILE)
    print "Error[ Total :%s Training: %s Test: %s]" % \
                (e_tot_reg[count],e_trn_reg[count],e_tst_reg[count])

    count = count + 1


k =  1
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 150693.672197 ]
Iteration : 1 ... done. Error:[ 101641.755951 ]
Iteration : 2 ... done. Error:[ 95079.6349238 ]
Iteration : 3 ... done. Error:[ 92583.7993911 ]
Iteration : 4 ... done. Error:[ 91335.8946456 ]
Iteration : 5 ... done. Error:[ 90619.5814071 ]
Iteration : 6 ... done. Error:[ 90172.4980125 ]
Iteration : 7 ... done. Error:[ 89877.7299946 ]
Iteration : 8 ... done. Error:[ 89675.83132 ]
Iteration : 9 ... done. Error:[ 89533.6077361 ]
Iteration : 10 ... done. Error:[ 89431.2146582 ]
Iteration : 11 ... done. Error:[ 89356.1707892 ]
Iteration : 12 ... done. Error:[ 89300.3188652 ]
Iteration : 13 ... done. Error:[ 89258.1684014 ]
Iteration : 14 ... done. Error:[ 89225.938433 ]
Iteration : 15 ... done. Error:[ 89200.9779743 ]
Iteration : 16 ... done. Error:[ 89181.4013604 ]
Iteration : 17 ... done. Error:[ 89165.8514182 ]
Iteration : 18 ... done. Error:[ 89153.3416282 ]
Iteration : 19 ... done. Error:[ 89143.1486937 ]
Iteration : 20 ... done. Error:[ 89134.7381677 ]
Iteration : 21 ... done. Error:[ 89127.7122549 ]
Iteration : 22 ... done. Error:[ 89121.7727682 ]
Iteration : 23 ... done. Error:[ 89116.6946023 ]
Iteration : 24 ... done. Error:[ 89112.3065944 ]
Iteration : 25 ... done. Error:[ 89108.4776263 ]
Iteration : 26 ... done. Error:[ 89105.1064744 ]
Iteration : 27 ... done. Error:[ 89102.1143591 ]
Iteration : 28 ... done. Error:[ 89099.4394493 ]
Iteration : 29 ... done. Error:[ 89097.032788 ]
Iteration : 30 ... done. Error:[ 89094.8552589 ]
Iteration : 31 ... done. Error:[ 89092.8753161 ]
Iteration : 32 ... done. Error:[ 89091.0672773 ]
Iteration : 33 ... done. Error:[ 89089.4100333 ]
Iteration : 34 ... done. Error:[ 89087.8860679 ]
Iteration : 35 ... done. Error:[ 89086.4807092 ]
Iteration : 36 ... done. Error:[ 89085.1815542 ]
Iteration : 37 ... done. Error:[ 89083.9780247 ]
Iteration : 38 ... done. Error:[ 89082.8610217 ]
Iteration : 39 ... done. Error:[ 89081.8226555 ]
Error[ Total :89081.8226555 Training: 87458.9675899 Test: 10461.8791935]
k =  2
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 121635.703723 ]
Iteration : 1 ... done. Error:[ 97008.0591559 ]
Iteration : 2 ... done. Error:[ 92727.9221718 ]
Iteration : 3 ... done. Error:[ 91112.3127497 ]
Iteration : 4 ... done. Error:[ 90297.0089831 ]
Iteration : 5 ... done. Error:[ 89801.6083687 ]
Iteration : 6 ... done. Error:[ 89451.8943007 ]
Iteration : 7 ... done. Error:[ 89167.0822569 ]
Iteration : 8 ... done. Error:[ 88899.909461 ]
Iteration : 9 ... done. Error:[ 88617.0806773 ]
Iteration : 10 ... done. Error:[ 88294.4746471 ]
Iteration : 11 ... done. Error:[ 87919.6689855 ]
Iteration : 12 ... done. Error:[ 87496.6778052 ]
Iteration : 13 ... done. Error:[ 87046.301537 ]
Iteration : 14 ... done. Error:[ 86598.2080708 ]
Iteration : 15 ... done. Error:[ 86179.0672695 ]
Iteration : 16 ... done. Error:[ 85804.9801815 ]
Iteration : 17 ... done. Error:[ 85481.1189349 ]
Iteration : 18 ... done. Error:[ 85205.3809613 ]
Iteration : 19 ... done. Error:[ 84972.2538676 ]
Iteration : 20 ... done. Error:[ 84775.3374135 ]
Iteration : 21 ... done. Error:[ 84608.5830013 ]
Iteration : 22 ... done. Error:[ 84466.7476628 ]
Iteration : 23 ... done. Error:[ 84345.4659011 ]
Iteration : 24 ... done. Error:[ 84241.1687936 ]
Iteration : 25 ... done. Error:[ 84150.9592881 ]
Iteration : 26 ... done. Error:[ 84072.4885931 ]
Iteration : 27 ... done. Error:[ 84003.8488985 ]
Iteration : 28 ... done. Error:[ 83943.4852865 ]
Iteration : 29 ... done. Error:[ 83890.1251661 ]
Iteration : 30 ... done. Error:[ 83842.7223181 ]
Iteration : 31 ... done. Error:[ 83800.4126563 ]
Iteration : 32 ... done. Error:[ 83762.4792214 ]
Iteration : 33 ... done. Error:[ 83728.3244051 ]
Iteration : 34 ... done. Error:[ 83697.4478313 ]
Iteration : 35 ... done. Error:[ 83669.4286704 ]
Iteration : 36 ... done. Error:[ 83643.9114415 ]
Iteration : 37 ... done. Error:[ 83620.5945664 ]
Iteration : 38 ... done. Error:[ 83599.2211066 ]
Iteration : 39 ... done. Error:[ 83579.5712358 ]
Error[ Total :83579.5712358 Training: 81856.041058 Test: 10003.5541156]
k =  3
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 115904.900661 ]
Iteration : 1 ... done. Error:[ 94694.9334649 ]
Iteration : 2 ... done. Error:[ 91083.4153258 ]
Iteration : 3 ... done. Error:[ 89640.1922566 ]
Iteration : 4 ... done. Error:[ 88804.8942173 ]
Iteration : 5 ... done. Error:[ 88170.7361759 ]
Iteration : 6 ... done. Error:[ 87597.2643479 ]
Iteration : 7 ... done. Error:[ 87042.7834697 ]
Iteration : 8 ... done. Error:[ 86510.6113649 ]
Iteration : 9 ... done. Error:[ 86018.8310393 ]
Iteration : 10 ... done. Error:[ 85581.962476 ]
Iteration : 11 ... done. Error:[ 85204.8558477 ]
Iteration : 12 ... done. Error:[ 84884.5272217 ]
Iteration : 13 ... done. Error:[ 84614.0666948 ]
Iteration : 14 ... done. Error:[ 84385.5647465 ]
Iteration : 15 ... done. Error:[ 84191.6073577 ]
Iteration : 16 ... done. Error:[ 84025.8179237 ]
Iteration : 17 ... done. Error:[ 83882.930386 ]
Iteration : 18 ... done. Error:[ 83758.6797041 ]
Iteration : 19 ... done. Error:[ 83649.6434284 ]
Iteration : 20 ... done. Error:[ 83553.0870236 ]
Iteration : 21 ... done. Error:[ 83466.8294547 ]
Iteration : 22 ... done. Error:[ 83389.1314873 ]
Iteration : 23 ... done. Error:[ 83318.6045071 ]
Iteration : 24 ... done. Error:[ 83254.136654 ]
Iteration : 25 ... done. Error:[ 83194.8332722 ]
Iteration : 26 ... done. Error:[ 83139.9691808 ]
Iteration : 27 ... done. Error:[ 83088.9507479 ]
Iteration : 28 ... done. Error:[ 83041.2861141 ]
Iteration : 29 ... done. Error:[ 82996.5621614 ]
Iteration : 30 ... done. Error:[ 82954.4269911 ]
Iteration : 31 ... done. Error:[ 82914.5768114 ]
Iteration : 32 ... done. Error:[ 82876.7462544 ]
Iteration : 33 ... done. Error:[ 82840.7012837 ]
Iteration : 34 ... done. Error:[ 82806.233992 ]
Iteration : 35 ... done. Error:[ 82773.1587391 ]
Iteration : 36 ... done. Error:[ 82741.3092153 ]
Iteration : 37 ... done. Error:[ 82710.5361372 ]
Iteration : 38 ... done. Error:[ 82680.7053774 ]
Iteration : 39 ... done. Error:[ 82651.6964011 ]
Error[ Total :82651.6964011 Training: 80879.3501149 Test: 9853.63545658]
k =  4
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 110417.320417 ]
Iteration : 1 ... done. Error:[ 93799.4211384 ]
Iteration : 2 ... done. Error:[ 90614.1455115 ]
Iteration : 3 ... done. Error:[ 89387.3469901 ]
Iteration : 4 ... done. Error:[ 88737.1137139 ]
Iteration : 5 ... done. Error:[ 88285.0810512 ]
Iteration : 6 ... done. Error:[ 87882.3624248 ]
Iteration : 7 ... done. Error:[ 87459.9056324 ]
Iteration : 8 ... done. Error:[ 86993.0523585 ]
Iteration : 9 ... done. Error:[ 86491.1181417 ]
Iteration : 10 ... done. Error:[ 85984.5903158 ]
Iteration : 11 ... done. Error:[ 85506.5677611 ]
Iteration : 12 ... done. Error:[ 85078.8253396 ]
Iteration : 13 ... done. Error:[ 84708.9088697 ]
Iteration : 14 ... done. Error:[ 84394.3946122 ]
Iteration : 15 ... done. Error:[ 84128.1564744 ]
Iteration : 16 ... done. Error:[ 83901.8766064 ]
Iteration : 17 ... done. Error:[ 83707.7510757 ]
Iteration : 18 ... done. Error:[ 83539.096427 ]
Iteration : 19 ... done. Error:[ 83390.4327401 ]
Iteration : 20 ... done. Error:[ 83257.3633742 ]
Iteration : 21 ... done. Error:[ 83136.4002603 ]
Iteration : 22 ... done. Error:[ 83024.795115 ]
Iteration : 23 ... done. Error:[ 82920.3965443 ]
Iteration : 24 ... done. Error:[ 82821.536057 ]
Iteration : 25 ... done. Error:[ 82726.9395827 ]
Iteration : 26 ... done. Error:[ 82635.659165 ]
Iteration : 27 ... done. Error:[ 82547.0194144 ]
Iteration : 28 ... done. Error:[ 82460.5739497 ]
Iteration : 29 ... done. Error:[ 82376.0679906 ]
Iteration : 30 ... done. Error:[ 82293.4042992 ]
Iteration : 31 ... done. Error:[ 82212.6107236 ]
Iteration : 32 ... done. Error:[ 82133.8085952 ]
Iteration : 33 ... done. Error:[ 82057.1820907 ]
Iteration : 34 ... done. Error:[ 81982.9493059 ]
Iteration : 35 ... done. Error:[ 81911.3361424 ]
Iteration : 36 ... done. Error:[ 81842.554154 ]
Iteration : 37 ... done. Error:[ 81776.7832707 ]
Iteration : 38 ... done. Error:[ 81714.1598945 ]
Iteration : 39 ... done. Error:[ 81654.7703547 ]
Error[ Total :81654.7703547 Training: 79850.71995 Test: 9713.25109266]
k =  5
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 107946.792653 ]
Iteration : 1 ... done. Error:[ 92640.7343011 ]
Iteration : 2 ... done. Error:[ 89745.066851 ]
Iteration : 3 ... done. Error:[ 88586.7151958 ]
Iteration : 4 ... done. Error:[ 87890.1125807 ]
Iteration : 5 ... done. Error:[ 87314.1440356 ]
Iteration : 6 ... done. Error:[ 86750.8135601 ]
Iteration : 7 ... done. Error:[ 86184.8448924 ]
Iteration : 8 ... done. Error:[ 85640.0607906 ]
Iteration : 9 ... done. Error:[ 85144.7988666 ]
Iteration : 10 ... done. Error:[ 84714.7387993 ]
Iteration : 11 ... done. Error:[ 84351.7325145 ]
Iteration : 12 ... done. Error:[ 84049.3566565 ]
Iteration : 13 ... done. Error:[ 83798.1272957 ]
Iteration : 14 ... done. Error:[ 83588.4736276 ]
Iteration : 15 ... done. Error:[ 83411.9791489 ]
Iteration : 16 ... done. Error:[ 83261.6988016 ]
Iteration : 17 ... done. Error:[ 83132.077183 ]
Iteration : 18 ... done. Error:[ 83018.732661 ]
Iteration : 19 ... done. Error:[ 82918.2235816 ]
Iteration : 20 ... done. Error:[ 82827.840556 ]
Iteration : 21 ... done. Error:[ 82745.4367519 ]
Iteration : 22 ... done. Error:[ 82669.2951246 ]
Iteration : 23 ... done. Error:[ 82598.0271214 ]
Iteration : 24 ... done. Error:[ 82530.4966393 ]
Iteration : 25 ... done. Error:[ 82465.7636601 ]
Iteration : 26 ... done. Error:[ 82403.0430252 ]
Iteration : 27 ... done. Error:[ 82341.6748293 ]
Iteration : 28 ... done. Error:[ 82281.1037495 ]
Iteration : 29 ... done. Error:[ 82220.8652527 ]
Iteration : 30 ... done. Error:[ 82160.5770574 ]
Iteration : 31 ... done. Error:[ 82099.9344919 ]
Iteration : 32 ... done. Error:[ 82038.7085441 ]
Iteration : 33 ... done. Error:[ 81976.7454675 ]
Iteration : 34 ... done. Error:[ 81913.9668505 ]
Iteration : 35 ... done. Error:[ 81850.3691095 ]
Iteration : 36 ... done. Error:[ 81786.0214688 ]
Iteration : 37 ... done. Error:[ 81721.061682 ]
Iteration : 38 ... done. Error:[ 81655.6890231 ]
Iteration : 39 ... done. Error:[ 81590.1544365 ]
Error[ Total :81590.1544365 Training: 79766.8507353 Test: 9861.60118988]
k =  6
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 106201.61713 ]
Iteration : 1 ... done. Error:[ 91912.2922258 ]
Iteration : 2 ... done. Error:[ 89249.2144161 ]
Iteration : 3 ... done. Error:[ 88235.9698817 ]
Iteration : 4 ... done. Error:[ 87649.4513187 ]
Iteration : 5 ... done. Error:[ 87165.4595776 ]
Iteration : 6 ... done. Error:[ 86672.5502496 ]
Iteration : 7 ... done. Error:[ 86145.033108 ]
Iteration : 8 ... done. Error:[ 85603.8365051 ]
Iteration : 9 ... done. Error:[ 85086.3632676 ]
Iteration : 10 ... done. Error:[ 84621.6558573 ]
Iteration : 11 ... done. Error:[ 84221.1123379 ]
Iteration : 12 ... done. Error:[ 83882.319745 ]
Iteration : 13 ... done. Error:[ 83596.3317846 ]
Iteration : 14 ... done. Error:[ 83352.8252448 ]
Iteration : 15 ... done. Error:[ 83142.5175108 ]
Iteration : 16 ... done. Error:[ 82957.8972814 ]
Iteration : 17 ... done. Error:[ 82793.2002497 ]
Iteration : 18 ... done. Error:[ 82644.131938 ]
Iteration : 19 ... done. Error:[ 82507.5510925 ]
Iteration : 20 ... done. Error:[ 82381.1873766 ]
Iteration : 21 ... done. Error:[ 82263.4107234 ]
Iteration : 22 ... done. Error:[ 82153.0504438 ]
Iteration : 23 ... done. Error:[ 82049.2571321 ]
Iteration : 24 ... done. Error:[ 81951.3999243 ]
Iteration : 25 ... done. Error:[ 81858.9922982 ]
Iteration : 26 ... done. Error:[ 81771.64035 ]
Iteration : 27 ... done. Error:[ 81689.0082347 ]
Iteration : 28 ... done. Error:[ 81610.7962329 ]
Iteration : 29 ... done. Error:[ 81536.7277195 ]
Iteration : 30 ... done. Error:[ 81466.542115 ]
Iteration : 31 ... done. Error:[ 81399.9916325 ]
Iteration : 32 ... done. Error:[ 81336.8402624 ]
Iteration : 33 ... done. Error:[ 81276.8639276 ]
Iteration : 34 ... done. Error:[ 81219.8511186 ]
Iteration : 35 ... done. Error:[ 81165.6035823 ]
Iteration : 36 ... done. Error:[ 81113.9368203 ]
Iteration : 37 ... done. Error:[ 81064.6802778 ]
Iteration : 38 ... done. Error:[ 81017.6771851 ]
Iteration : 39 ... done. Error:[ 80972.7840617 ]
Error[ Total :80972.7840617 Training: 79131.0316698 Test: 9820.99851529]
k =  7
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 102644.124858 ]
Iteration : 1 ... done. Error:[ 90387.1065459 ]
Iteration : 2 ... done. Error:[ 87899.644212 ]
Iteration : 3 ... done. Error:[ 86774.3148543 ]
Iteration : 4 ... done. Error:[ 86011.78077 ]
Iteration : 5 ... done. Error:[ 85389.4217978 ]
Iteration : 6 ... done. Error:[ 84856.0540219 ]
Iteration : 7 ... done. Error:[ 84402.0106286 ]
Iteration : 8 ... done. Error:[ 84021.8168661 ]
Iteration : 9 ... done. Error:[ 83706.546342 ]
Iteration : 10 ... done. Error:[ 83445.1984561 ]
Iteration : 11 ... done. Error:[ 83227.0072655 ]
Iteration : 12 ... done. Error:[ 83042.6691898 ]
Iteration : 13 ... done. Error:[ 82884.6423382 ]
Iteration : 14 ... done. Error:[ 82746.9931456 ]
Iteration : 15 ... done. Error:[ 82625.1019038 ]
Iteration : 16 ... done. Error:[ 82515.3696091 ]
Iteration : 17 ... done. Error:[ 82414.9743096 ]
Iteration : 18 ... done. Error:[ 82321.6841304 ]
Iteration : 19 ... done. Error:[ 82233.7197786 ]
Iteration : 20 ... done. Error:[ 82149.6560971 ]
Iteration : 21 ... done. Error:[ 82068.352942 ]
Iteration : 22 ... done. Error:[ 81988.9073677 ]
Iteration : 23 ... done. Error:[ 81910.6207674 ]
Iteration : 24 ... done. Error:[ 81832.9759453 ]
Iteration : 25 ... done. Error:[ 81755.6201084 ]
Iteration : 26 ... done. Error:[ 81678.3505573 ]
Iteration : 27 ... done. Error:[ 81601.100562 ]
Iteration : 28 ... done. Error:[ 81523.9236298 ]
Iteration : 29 ... done. Error:[ 81446.9751461 ]
Iteration : 30 ... done. Error:[ 81370.4911917 ]
Iteration : 31 ... done. Error:[ 81294.7651191 ]
Iteration : 32 ... done. Error:[ 81220.123107 ]
Iteration : 33 ... done. Error:[ 81146.900293 ]
Iteration : 34 ... done. Error:[ 81075.4191521 ]
Iteration : 35 ... done. Error:[ 81005.9715478 ]
Iteration : 36 ... done. Error:[ 80938.8054124 ]
Iteration : 37 ... done. Error:[ 80874.11643 ]
Iteration : 38 ... done. Error:[ 80812.0445243 ]
Iteration : 39 ... done. Error:[ 80752.6745054 ]
Error[ Total :80752.6745054 Training: 78919.7303984 Test: 9755.18900647]
k =  8
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 102074.61635 ]
Iteration : 1 ... done. Error:[ 90360.1833759 ]
Iteration : 2 ... done. Error:[ 88154.6634335 ]
Iteration : 3 ... done. Error:[ 87192.9303251 ]
Iteration : 4 ... done. Error:[ 86515.96328 ]
Iteration : 5 ... done. Error:[ 85914.2348954 ]
Iteration : 6 ... done. Error:[ 85354.0025902 ]
Iteration : 7 ... done. Error:[ 84848.8044157 ]
Iteration : 8 ... done. Error:[ 84412.4729199 ]
Iteration : 9 ... done. Error:[ 84046.5257967 ]
Iteration : 10 ... done. Error:[ 83743.2099688 ]
Iteration : 11 ... done. Error:[ 83491.2622314 ]
Iteration : 12 ... done. Error:[ 83279.5532841 ]
Iteration : 13 ... done. Error:[ 83098.5480132 ]
Iteration : 14 ... done. Error:[ 82940.5805566 ]
Iteration : 15 ... done. Error:[ 82799.6664304 ]
Iteration : 16 ... done. Error:[ 82671.199535 ]
Iteration : 17 ... done. Error:[ 82551.6661787 ]
Iteration : 18 ... done. Error:[ 82438.4128107 ]
Iteration : 19 ... done. Error:[ 82329.4683382 ]
Iteration : 20 ... done. Error:[ 82223.4108908 ]
Iteration : 21 ... done. Error:[ 82119.2670898 ]
Iteration : 22 ... done. Error:[ 82016.4331371 ]
Iteration : 23 ... done. Error:[ 81914.6092088 ]
Iteration : 24 ... done. Error:[ 81813.7410335 ]
Iteration : 25 ... done. Error:[ 81713.9649341 ]
Iteration : 26 ... done. Error:[ 81615.5548412 ]
Iteration : 27 ... done. Error:[ 81518.8716273 ]
Iteration : 28 ... done. Error:[ 81424.3163593 ]
Iteration : 29 ... done. Error:[ 81332.2895794 ]
Iteration : 30 ... done. Error:[ 81243.1585319 ]
Iteration : 31 ... done. Error:[ 81157.2335115 ]
Iteration : 32 ... done. Error:[ 81074.7535195 ]
Iteration : 33 ... done. Error:[ 80995.8804415 ]
Iteration : 34 ... done. Error:[ 80920.7002453 ]
Iteration : 35 ... done. Error:[ 80849.229321 ]
Iteration : 36 ... done. Error:[ 80781.4240559 ]
Iteration : 37 ... done. Error:[ 80717.1919564 ]
Iteration : 38 ... done. Error:[ 80656.4029927 ]
Iteration : 39 ... done. Error:[ 80598.9002444 ]
Error[ Total :80598.9002444 Training: 78745.5525366 Test: 9708.77255274]
k =  9
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 101918.391961 ]
Iteration : 1 ... done. Error:[ 90035.9841626 ]
Iteration : 2 ... done. Error:[ 87600.4905252 ]
Iteration : 3 ... done. Error:[ 86550.1833388 ]
Iteration : 4 ... done. Error:[ 85840.0770676 ]
Iteration : 5 ... done. Error:[ 85224.4922571 ]
Iteration : 6 ... done. Error:[ 84653.7572369 ]
Iteration : 7 ... done. Error:[ 84135.087281 ]
Iteration : 8 ... done. Error:[ 83681.2883357 ]
Iteration : 9 ... done. Error:[ 83295.0172044 ]
Iteration : 10 ... done. Error:[ 82969.8470498 ]
Iteration : 11 ... done. Error:[ 82695.4062096 ]
Iteration : 12 ... done. Error:[ 82461.1400007 ]
Iteration : 13 ... done. Error:[ 82258.0269027 ]
Iteration : 14 ... done. Error:[ 82079.0055154 ]
Iteration : 15 ... done. Error:[ 81918.8161989 ]
Iteration : 16 ... done. Error:[ 81773.6525932 ]
Iteration : 17 ... done. Error:[ 81640.8001858 ]
Iteration : 18 ... done. Error:[ 81518.3269296 ]
Iteration : 19 ... done. Error:[ 81404.8413426 ]
Iteration : 20 ... done. Error:[ 81299.3139906 ]
Iteration : 21 ... done. Error:[ 81200.9517327 ]
Iteration : 22 ... done. Error:[ 81109.1129251 ]
Iteration : 23 ... done. Error:[ 81023.2527492 ]
Iteration : 24 ... done. Error:[ 80942.8896125 ]
Iteration : 25 ... done. Error:[ 80867.5855683 ]
Iteration : 26 ... done. Error:[ 80796.93557 ]
Iteration : 27 ... done. Error:[ 80730.5619677 ]
Iteration : 28 ... done. Error:[ 80668.1118898 ]
Iteration : 29 ... done. Error:[ 80609.2560556 ]
Iteration : 30 ... done. Error:[ 80553.6881749 ]
Iteration : 31 ... done. Error:[ 80501.1244839 ]
Iteration : 32 ... done. Error:[ 80451.3032048 ]
Iteration : 33 ... done. Error:[ 80403.983849 ]
Iteration : 34 ... done. Error:[ 80358.9463566 ]
Iteration : 35 ... done. Error:[ 80315.9900943 ]
Iteration : 36 ... done. Error:[ 80274.932751 ]
Iteration : 37 ... done. Error:[ 80235.6091651 ]
Iteration : 38 ... done. Error:[ 80197.8701224 ]
Iteration : 39 ... done. Error:[ 80161.5811493 ]
Error[ Total :80161.5811493 Training: 78304.499943 Test: 9715.4972592]
k =  10
Running SGD for 40 iterations.
Iteration : 0 ... done. Error:[ 101370.473754 ]
Iteration : 1 ... done. Error:[ 89340.1119343 ]
Iteration : 2 ... done. Error:[ 87055.0668611 ]
Iteration : 3 ... done. Error:[ 86074.5711487 ]
Iteration : 4 ... done. Error:[ 85385.2381397 ]
Iteration : 5 ... done. Error:[ 84773.1446809 ]
Iteration : 6 ... done. Error:[ 84209.3818129 ]
Iteration : 7 ... done. Error:[ 83707.6733048 ]
Iteration : 8 ... done. Error:[ 83277.8577642 ]
Iteration : 9 ... done. Error:[ 82917.278542 ]
Iteration : 10 ... done. Error:[ 82615.7478687 ]
Iteration : 11 ... done. Error:[ 82361.2745145 ]
Iteration : 12 ... done. Error:[ 82143.1229215 ]
Iteration : 13 ... done. Error:[ 81952.7714266 ]
Iteration : 14 ... done. Error:[ 81783.857551 ]
Iteration : 15 ... done. Error:[ 81631.7826954 ]
Iteration : 16 ... done. Error:[ 81493.2756603 ]
Iteration : 17 ... done. Error:[ 81366.0186398 ]
Iteration : 18 ... done. Error:[ 81248.3572006 ]
Iteration : 19 ... done. Error:[ 81139.0869846 ]
Iteration : 20 ... done. Error:[ 81037.3024421 ]
Iteration : 21 ... done. Error:[ 80942.2929311 ]
Iteration : 22 ... done. Error:[ 80853.4736825 ]
Iteration : 23 ... done. Error:[ 80770.3416238 ]
Iteration : 24 ... done. Error:[ 80692.4483314 ]
Iteration : 25 ... done. Error:[ 80619.3843309 ]
Iteration : 26 ... done. Error:[ 80550.7705649 ]
Iteration : 27 ... done. Error:[ 80486.2541191 ]
Iteration : 28 ... done. Error:[ 80425.5062691 ]
Iteration : 29 ... done. Error:[ 80368.2216256 ]
Iteration : 30 ... done. Error:[ 80314.1176576 ]
Iteration : 31 ... done. Error:[ 80262.9342057 ]
Iteration : 32 ... done. Error:[ 80214.4328142 ]
Iteration : 33 ... done. Error:[ 80168.3958343 ]
Iteration : 34 ... done. Error:[ 80124.6253195 ]
Iteration : 35 ... done. Error:[ 80082.9417658 ]
Iteration : 36 ... done. Error:[ 80043.1827565 ]
Iteration : 37 ... done. Error:[ 80005.2015709 ]
Iteration : 38 ... done. Error:[ 79968.865802 ]
Iteration : 39 ... done. Error:[ 79934.0560221 ]
Error[ Total :79934.0560221 Training: 78065.4881469 Test: 9676.96069794]

In [22]:
x = pylab.arange(0,len(K))

f2 = pylab.figure(figsize=(9,9))
p1 = f2.add_subplot(211)
p1.plot(x, e_trn_reg)
p1.set_xlabel("Iteration")
p1.set_ylabel("Training Error")
p1.set_title("Regularization : Training Error vs. Iterations")

p2 = f2.add_subplot(212)
p2.plot(x, e_tst_reg)
p2.set_xlabel("Iteration")
p2.set_ylabel("Test Error")
p2.set_title("Regularization : Test Error vs. Iterations")


Out[22]:
<matplotlib.text.Text at 0x107545710>

In [24]:
f3 = pylab.figure(figsize=(9,9))
p1 = f3.add_subplot(211)
p1.plot(x, e_trn)
p1.plot(x, e_trn_reg)
p1.set_xlabel("Iteration")
p1.set_ylabel("Training Error")
p1.set_title("Training Error vs. Iterations")

p2 = f3.add_subplot(212)
p2.plot(x, e_tst)
p2.plot(x, e_tst_reg)
p2.set_xlabel("Iteration")
p2.set_ylabel("Test Error")
p2.set_title("Test Error vs. Iterations")


Out[24]:
<matplotlib.text.Text at 0x107b11890>

In [ ]: